/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:JiveMessenger"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.SampleData;
using JiveMessenger.User;
using Microsoft.Practices.ServiceLocation;
using System;
using System.ComponentModel;
using System.Windows;

namespace JiveMessenger.ViewModels
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator : INotifyPropertyChanged
    {
        private string _viewModelAddGroupKey = System.Guid.NewGuid().ToString();
        public string ViewModelAddGroupKey
        {
            get { return _viewModelAddGroupKey; }
            set
            {
                if (value != _viewModelAddGroupKey)
                {
                    _viewModelAddGroupKey = value;
                    RaisePropertyChanged("ViewModelAddGroup");
                }
            }
        }

        private string _viewModelAddEmoticonKey = System.Guid.NewGuid().ToString();
        public string ViewModelAddEmoticonKey
        {
            get { return _viewModelAddEmoticonKey; }
            set
            {
                if (value != _viewModelAddEmoticonKey)
                {
                    _viewModelAddEmoticonKey = value;
                    RaisePropertyChanged("ViewModelAddEmoticon");
                }
            }
        }

        private string _viewModelGetUserEmoticonsKey = System.Guid.NewGuid().ToString();
        public string ViewModelGetUserEmoticonsKey
        {
            get { return _viewModelGetUserEmoticonsKey; }
            set
            {
                if (value != _viewModelGetUserEmoticonsKey)
                {
                    _viewModelGetUserEmoticonsKey = value;
                    RaisePropertyChanged("ViewModelGetUserEmoticons");
                }
            }
        }

        private string _viewModelMainWindowKey = System.Guid.NewGuid().ToString();
        public string ViewModelMainWindowKey
        {
            get { return _viewModelMainWindowKey; }
            set
            {
                if (value != _viewModelMainWindowKey)
                {
                    _viewModelMainWindowKey = value;
                    RaisePropertyChanged("ViewModelMainWindow");
                }
            }
        }

        private string _viewModelContactListKey = System.Guid.NewGuid().ToString();
        public string ViewModelContactListKey
        {
            get { return _viewModelContactListKey; }
            set
            {
                if (value != _viewModelContactListKey)
                {
                    _viewModelContactListKey = value;
                    RaisePropertyChanged("ViewModelContactList");
                }
            }
        }

        private string _viewModelLoginControlKey = System.Guid.NewGuid().ToString();
        public string ViewModelLoginControlKey
        {
            get { return _viewModelLoginControlKey; }
            set
            {
                if (value != _viewModelLoginControlKey)
                {
                    _viewModelLoginControlKey = value;
                    RaisePropertyChanged("ViewModelLoginControl");
                }
            }
        }

        private string _currentClientKey = System.Guid.NewGuid().ToString();
        public string CurrentClientKey
        {
            get { return _currentClientKey; }
            set
            {
                if (value != _currentClientKey)
                {
                    _currentClientKey = value;
                    RaisePropertyChanged("CurrentClient");
                }
            }
        }

        private static string _viewModelNotificationsKey = System.Guid.NewGuid().ToString();
        public string ViewModelNotificationsKey
        {
            get { return _viewModelNotificationsKey; }
            set
            {
                if (value != _viewModelNotificationsKey)
                {
                    _viewModelNotificationsKey = value;
                    RaisePropertyChanged("ViewModelNotificationsKey");
                }
            }
        }

        private string _viewModelRegisterWindowKey = System.Guid.NewGuid().ToString();
        public string ViewModelRegisterWindowKey
        {
            get { return _viewModelRegisterWindowKey; }
            set
            {
                if (value != _viewModelRegisterWindowKey)
                {
                    _viewModelRegisterWindowKey = value;
                    RaisePropertyChanged("ViewModelRegisterWindow");
                }
            }
        }

        private string _viewModelResetPasswordKey = System.Guid.NewGuid().ToString();
        public string ViewModelResetPasswordKey
        {
            get { return _viewModelResetPasswordKey; }
            set
            {
                if (value != _viewModelResetPasswordKey)
                {
                    _viewModelResetPasswordKey = value;
                    RaisePropertyChanged("ViewModelResetPassword");
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                // Create design time view services and models
                if (!SimpleIoc.Default.IsRegistered<IClient>())
                    SimpleIoc.Default.Register<IClient>(() => new SampleClient());

                if (!SimpleIoc.Default.IsRegistered<IViewModelContactList>())
                    SimpleIoc.Default.Register<IViewModelContactList>(() => new SampleViewModelContactList());

                if (!SimpleIoc.Default.IsRegistered<IViewModelMainWindow>())
                    SimpleIoc.Default.Register<IViewModelMainWindow>(() => new SampleViewModelMainWindow());

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddEmoticon>())
                    SimpleIoc.Default.Register<ViewModelAddEmoticon>(() => new ViewModelAddEmoticon(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelLoginControl>())
                    SimpleIoc.Default.Register<IViewModelLoginControl>(() => new ViewModelLoginControl(null));

                if (!SimpleIoc.Default.IsRegistered<IViewModelContactList>())
                    SimpleIoc.Default.Register<IViewModelContactList>(() => new ViewModelContactList(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddGroup>())
                    SimpleIoc.Default.Register<ViewModelAddGroup>(() => new ViewModelAddGroup(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelNotifications>())
                    SimpleIoc.Default.Register<IViewModelNotifications>(() => new SampleViewModelNotifications());

                if (!SimpleIoc.Default.IsRegistered<ViewModelRegisterWindow>())
                    SimpleIoc.Default.Register<ViewModelRegisterWindow>(() => new ViewModelRegisterWindow(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelResetPasswordWindow>())
                    SimpleIoc.Default.Register<ViewModelResetPasswordWindow>(() => new ViewModelResetPasswordWindow(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelManageGroup>())
                    SimpleIoc.Default.Register<IViewModelManageGroup>(()=>new SampleViewModelManageGroup());

                //if (!SimpleIoc.Default.IsRegistered<IViewModelImagePreviewWindow>())
                //    SimpleIoc.Default.Register<IViewModelImagePreviewWindow>(() => new SampleViewModelImagePreviewWindow());

                if (!SimpleIoc.Default.IsRegistered<IViewModelDownloadProgress>())
                    SimpleIoc.Default.Register<IViewModelDownloadProgress>(() => new SampleViewModelDownloadProgress());

                if (!SimpleIoc.Default.IsRegistered<IViewModelApplicationUpdate>())
                    SimpleIoc.Default.Register<IViewModelApplicationUpdate>(() => new SampleViewModelApplicationUpdate(CurrentClient), true);
            }
            else
            {

                // Create run time view services and models
                if (!SimpleIoc.Default.IsRegistered<ViewModelGetUserEmoticons>())
                    SimpleIoc.Default.Register<ViewModelGetUserEmoticons>(() => new ViewModelGetUserEmoticons(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelMainWindow>())
                    SimpleIoc.Default.Register<IViewModelMainWindow>(() => new ViewModelMainWindow(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddEmoticon>())
                    SimpleIoc.Default.Register<ViewModelAddEmoticon>(() => new ViewModelAddEmoticon(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IClient>())
                    SimpleIoc.Default.Register<IClient>(() => new Client());

                if (!SimpleIoc.Default.IsRegistered<IViewModelContactList>())
                    SimpleIoc.Default.Register<IViewModelContactList>(() => new ViewModelContactList(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelLoginControl>())
                    SimpleIoc.Default.Register<IViewModelLoginControl>(() => new ViewModelLoginControl(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelAddGroup>())
                    SimpleIoc.Default.Register<ViewModelAddGroup>(() => new ViewModelAddGroup(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelNotifications>())
                    SimpleIoc.Default.Register<IViewModelNotifications>(() => new ViewModelNotifications(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelRegisterWindow>())
                    SimpleIoc.Default.Register<ViewModelRegisterWindow>(() => new ViewModelRegisterWindow(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<ViewModelResetPasswordWindow>())
                    SimpleIoc.Default.Register<ViewModelResetPasswordWindow>(() => new ViewModelResetPasswordWindow(CurrentClient));

                if (!SimpleIoc.Default.IsRegistered<IViewModelManageGroup>())
                    SimpleIoc.Default.Register<IViewModelManageGroup>(() => new SampleData.SampleViewModelManageGroup());

                //if (!SimpleIoc.Default.IsRegistered<IViewModelImagePreviewWindow>())
                //    SimpleIoc.Default.Register<IViewModelImagePreviewWindow>(() => new ViewModelImagePreviewWindow());

                if (!SimpleIoc.Default.IsRegistered<IViewModelDownloadProgress>())
                    SimpleIoc.Default.Register<IViewModelDownloadProgress>(() => new ViewModelDownloadProgress(),true);
                
                if(!SimpleIoc.Default.IsRegistered<IViewModelApplicationUpdate>())
                    SimpleIoc.Default.Register<IViewModelApplicationUpdate>(() => new ViewModelApplicationUpdate(CurrentClient), true);
            }
        }

        public IViewModelApplicationUpdate ViewModelApplicationUpdate
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelApplicationUpdate>();
            }
        }

        public IViewModelDownloadProgress ViewModelDownloadProgress
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelDownloadProgress>();
            }
        }

        //public IViewModelImagePreviewWindow ViewModelImagePreviewWindow
        //{
        //    get
        //    {
        //        return ServiceLocator.Current.GetInstance<IViewModelImagePreviewWindow>();
        //    }
        //}


        public IViewModelManageGroup SampleViewModelManageGroup
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelManageGroup>();
            }
        }

        public ViewModelResetPasswordWindow ViewModelResetPasswordWindow
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelResetPasswordWindow>(ViewModelResetPasswordKey);
            }
        }

        public ViewModelRegisterWindow ViewModelRegisterWindow
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelRegisterWindow>(ViewModelRegisterWindowKey);
            }
        }

        public IViewModelNotifications ViewModelNotifications
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelNotifications>(ViewModelNotificationsKey);
            }
        }

        public ViewModelAddGroup ViewModelAddGroup
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelAddGroup>(ViewModelAddGroupKey);
            }
        }

        public IViewModelContactList ViewModelContactList
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelContactList>(ViewModelContactListKey);
            }
        }

        public IViewModelLoginControl ViewModelLoginControl
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelLoginControl>(ViewModelLoginControlKey);
            }
        }

        public ViewModelGetUserEmoticons ViewModelGetUserEmoticons
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelGetUserEmoticons>(ViewModelGetUserEmoticonsKey);
            }
        }

        public ViewModelAddEmoticon ViewModelAddEmoticon
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ViewModelAddEmoticon>(ViewModelAddEmoticonKey);
            }
        }
        public IClient CurrentClient
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IClient>(CurrentClientKey);
            }
        }
        public IViewModelMainWindow ViewModelMainWindow
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IViewModelMainWindow>(ViewModelMainWindowKey);
            }
        }

        public static void Cleanup()
        {
            // CleanupCurrentClient();
            // CleanupViewModelAddEmoticons();
            // CleanupViewModelGetUserEmoticons();
            // CleanupViewModelOpenLogs();
            // CleanupViewModelMainWindow();
            // CleanupViewModelContactList();



            // Messenger.Reset();
        }
        public static void CleanupCurrentClient()
        {
            var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];
            viewModelLocator.CurrentClient.Cleanup();

        }

        public static void CleanupViewModelContactList()
        {
            var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];
            viewModelLocator.ViewModelContactList.Cleanup();
        }

        public static void CleanupViewModelMainWindow()
        {
            var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];
            viewModelLocator.ViewModelMainWindow.Cleanup();
        }

        public static void CleanupViewModelGetUserEmoticons()
        {
            var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];
            viewModelLocator.ViewModelGetUserEmoticons.Cleanup();
        }

        public static void CleanupViewModelAddEmoticons()
        {
            var viewModelLocator = (ViewModelLocator)Application.Current.Resources["Locator"];
            viewModelLocator.ViewModelAddEmoticon.Cleanup();
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}